eststo clear


if("`c(os)'"=="Windows"){
	local tableSaveDir = "Tables\"
	local plotSaveDir = "Plots\"
	local pythonSaveDir = "PythonScripts\"
}
else{
	local tableSaveDir = "Tables/"
	local plotSaveDir = "Plots/"
	local pythonSaveDir = "PythonScripts/"
}

//Establish Control Variable Sets
//Spec1 controls are the same for both registrations and VMT 
local spec1Controls = "c.normYear#i.stateGroup c.quadYear#i.stateGroup asinhMetroPop asinhNonMetroPop asinhMetroInc asinhNonMetroInc logrealmeangasprice logemployment loglicenseddrivers logrealstategdp logroadmileage"
local spec1LinearControls = "c.normYear#i.stateGroup asinhMetroPop asinhNonMetroPop asinhMetroInc asinhNonMetroInc logrealmeangasprice logemployment loglicenseddrivers logrealstategdp logroadmileage"

//Other controls are not the same for both registrations and VMT 
//Registration Controls
local spec2RegControls = "`spec1Controls' i.transactionDataDummy"
local spec3RegControls = "`spec2RegControls' i.regDummyExtSpec3"
local spec4RegControls = "`spec2RegControls' i.regDummyExtSpec4"

local spec2RegLinearControls = "`spec1LinearControls' i.transactionDataDummy"
local spec3RegLinearControls = "`spec2RegLinearControls' i.regDummyExtSpec3"
local spec4RegLinearControls = "`spec2RegLinearControls' i.regDummyExtSpec4"


di "`spec3RegControls'"

//VMT Controls
local spec2VMTControls = "`spec1Controls'"
local spec3VMTControls = "`spec2VMTControls' i.vmtDummyExtSpec3"
local spec4VMTControls = "`spec2VMTControls' i.vmtDummyExtSpec4"

local spec2VMTLinearControls = "`spec1LinearControls'"
local spec3VMTLinearControls = "`spec2VMTLinearControls' i.vmtDummyExtSpec3"
local spec4VMTLinearControls = "`spec2VMTLinearControls' i.vmtDummyExtSpec4"

//Absorb Vars are always time and state fixed effects
local absorbVars = "ib51.stateGroup ib1995.year"

//Establish Cluster Variable
local clusterVar = "stateGroup year"

//Establish primary null
local primaryNull = 1.6
local altNull = 1
local altNull2 = 0.3
local altNull3 = 0
local altNull4 = -2.5


program LOO
version 16.1
	gettoken fsVars 0:0, parse(",")
	gettoken com 0:0, parse(",")
	gettoken rfVars 0:0, parse(",")
	gettoken com 0:0, parse(",")
	gettoken clusterVar 0:0, parse(",")
	gettoken com 0:0, parse(",")
	gettoken treatmentVar 0:0, parse(",")
	gettoken com 0:0, parse(",")
	gettoken modelTitle 0:0, parse(",")
	gettoken com 0:0, parse(",")
	gettoken primaryNull 0:0, parse(",")
	
	qui putexcel set "Tables/iv.xlsx", sheet("LOO", replace) modify
	
	tempname null tempPi tempDelta tempPiVar tempDeltaVar tempCovar
	
	local index = 2
	
	qui putexcel A1 = "State Removed"
	qui putexcel B1 = "Pi"
	qui putexcel C1 = "Pi Variance"
	qui putexcel D1 = "Delta"
	qui putexcel E1 = "Delta Variance"
	qui putexcel F1 = "Pi-Delta Covariance"
	qui putexcel G1 = "AR(Beta=`primaryNull')"
	qui putexcel H1 = "P-Value(Beta=`primaryNull')"
	
	qui levelsof statecode
	foreach tempSC in `r(levels)'{
		preserve
		di 
		qui keep if statecode=="`tempSC'"
		
			qui putexcel A`index' = "`tempSC'"
			
			jointEstimate `fsVars' if statecode=="`tempSC'", `rfVars', `clusterVar', `treatmentVar', "Working on `modelTitle' LOO w/o `tempSC'", `tempSC'
	
			scalar `tempPi' = `r(pi)'
			scalar `tempDelta' = `r(delta)'
			scalar `tempPiVar' = `r(piVar)'
			scalar `tempDeltaVar' = `r(deltaVar)'
			scalar `tempCovar' = `r(covar)'
			
			qui putexcel B`index' = `tempPi'
			qui putexcel C`index' = `tempPiVar'
			qui putexcel D`index' = `tempDelta'
			qui putexcel E`index' = `tempDeltaVar'
			qui putexcel F`index' = `tempCovar'
			
			testNull `tempPi' `tempDelta' `tempPiVar' `tempDeltaVar' `tempCovar' `primaryNull'
			
			qui putexcel G`index' = `r(arStat)'
			qui putexcel H`index' = `r(pVal)'
			
		local index = `index'+1
		restore
	}
	

	
	
	
end


local treatmentVar = "1.nosafetyind"

local fsOut = "logregistrations"
local rfOut = "logvmt"

local fsOutPerCapita = "logRegPerCapita"
local rfOutPerCapita = "logVMTPerCapita"

local fsControlsSpec1 = "`spec1Controls'"
local rfControlsSpec1 = "`spec1Controls'"

local fsControlsSpec1Linear = "`spec1LinearControls'"
local rfControlsSpec1Linear = "`spec1LinearControls'"

local treatControl = "nosafetyind"

local modelTitleSpec1 = "Spec1"
local modelTitleSpec1PerCapita = "Spec1PerCapita"
local modelTitleSpec1Linear = "Spec1Linear"
local nullLowSpec1 = -3.6
local nullDeltaSpec1 = 0.001
local nullHighSpec1 = 1


runModelFWL `fsOut', `rfOut', `fsControlsSpec1', `rfControlsSpec1', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec1', `nullLowSpec1', `nullDeltaSpec1', `nullHighSpec1', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'
runModelFWL `fsOut', `rfOut', `fsControlsSpec1Linear', `rfControlsSpec1Linear', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec1Linear', `nullLowSpec1', `nullDeltaSpec1', `nullHighSpec1', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'
runModelFWL `fsOutPerCapita', `rfOutPerCapita', `fsControlsSpec1', `rfControlsSpec1', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec1PerCapita', `nullLowSpec1', `nullDeltaSpec1', `nullHighSpec1', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'

local fsControlsSpec2 = "`spec2RegControls'"
local rfControlsSpec2 = "`spec2VMTControls'"

local fsControlsSpec2Linear = "`spec2RegLinearControls'"
local rfControlsSpec2Linear = "`spec2VMTLinearControls'"

local treatControl = "nosafetyind"

local modelTitleSpec2 = "Spec2"
local modelTitleSpec2PerCapita = "Spec2PerCapita"
local modelTitleSpec2Linear = "Spec2Linear"
local nullLowSpec2 = -4
local nullDeltaSpec2 = 0.001
local nullHighSpec2 = 1

runModelFWL `fsOut', `rfOut', `fsControlsSpec2', `rfControlsSpec2', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec2', `nullLowSpec2', `nullDeltaSpec2', `nullHighSpec2', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'
runModelFWL `fsOut', `rfOut', `fsControlsSpec2Linear', `rfControlsSpec2Linear', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec2Linear', `nullLowSpec2', `nullDeltaSpec2', `nullHighSpec2', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'
runModelFWL `fsOutPerCapita', `rfOutPerCapita', `fsControlsSpec2', `rfControlsSpec2', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec2PerCapita', `nullLowSpec2', `nullDeltaSpec2', `nullHighSpec2', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'

local fsControlsSpec3 = "`spec3RegControls'"
local rfControlsSpec3 = "`spec3VMTControls'"

local fsControlsSpec3Linear = "`spec3RegLinearControls'"
local rfControlsSpec3Linear = "`spec3VMTLinearControls'"

local treatControl = "nosafetyind"

local modelTitleSpec3 = "Spec3"
local modelTitleSpec3PerCapita = "Spec3PerCapita"
local modelTitleSpec3Linear = "Spec3Linear"
local nullLowSpec3 = -8
local nullDeltaSpec3 = 0.001
local nullHighSpec3 = 5

runModelFWL `fsOut', `rfOut', `fsControlsSpec3', `rfControlsSpec3', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec3', `nullLowSpec3', `nullDeltaSpec3', `nullHighSpec3', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'
runModelFWL `fsOut', `rfOut', `fsControlsSpec3Linear', `rfControlsSpec3Linear', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec3Linear', `nullLowSpec3', `nullDeltaSpec3', `nullHighSpec3', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'
runModelFWL `fsOutPerCapita', `rfOutPerCapita', `fsControlsSpec3', `rfControlsSpec3', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec3PerCapita', `nullLowSpec3', `nullDeltaSpec3', `nullHighSpec3', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'

local fsControlsSpec4 = "`spec4RegControls'"
local rfControlsSpec4 = "`spec4VMTControls'"

local fsControlsSpec4Linear = "`spec4RegLinearControls'"
local rfControlsSpec4Linear = "`spec4VMTLinearControls'"

local treatControl = "nosafetyind"

local modelTitleSpec4 = "Spec4"
local modelTitleSpec4PerCapita = "Spec4PerCapita"
local modelTitleSpec4Linear = "Spec4Linear" 
local nullLowSpec4 = -50
local nullDeltaSpec4 = 0.01
local nullHighSpec4 = 40

runModelFWL `fsOut', `rfOut', `fsControlsSpec4', `rfControlsSpec4', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec4', `nullLowSpec4', `nullDeltaSpec4', `nullHighSpec4', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'
runModelFWL `fsOut', `rfOut', `fsControlsSpec4Linear', `rfControlsSpec4Linear', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec4Linear', `nullLowSpec4', `nullDeltaSpec4', `nullHighSpec4', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'
runModelFWL `fsOutPerCapita', `rfOutPerCapita', `fsControlsSpec4', `rfControlsSpec4', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec4PerCapita', `nullLowSpec4', `nullDeltaSpec4', `nullHighSpec4', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'

//Combine all of the IV information
python script "`pythonSaveDir'combineIV.py"

//Output tables
constructTable VMTPerCapitaiv; Fleet Travel Distance Per Capita; Spec1PerCapita Spec2PerCapita Spec3PerCapita Spec4PerCapita; `primaryNull'; `altNull'; `altNull2'; `altNull3'; `altNull4'
constructTable VMTiv; Fleet Travel Distance; Spec1 Spec2 Spec3 Spec4; `primaryNull'; `altNull'; `altNull2'; `altNull3'; `altNull4'
constructTable VMTivShort; Fleet Travel Distance; Spec1 Spec2; `primaryNull'; `altNull'; `altNull2'; `altNull3'; `altNull4'

